<template>
	<view>
	<custom-navbar title="文件列表"></custom-navbar>
	<view class="fileList">
		<CardWrap>
			<view class="title">
				送出文书
			</view>
			<FileWrap :fileName="item.fileOriginalName" :fileType="item.fileType"  :timeDate="item.timeDate" v-for="(item,index) in sendFileList" :key="index" @tap="previewFile(item)" :isNeedReceipt="signReceiptFile" :isRead="roleInfo === 'BureauOfJustice' ?item.isScan : item.isRead" :isReceipt="isReceipt"></FileWrap>
		</CardWrap>
	    <CardWrap v-if="roleInfo == 'Procuratorate'">
		<view class="title">
			送达回证
		</view>
		<FileWrap 
		:fileName="revice.fileOriginalName" 
		:fileType="revice.fileType" 
		:pageNum="1" 
		saveCapacity="268 KB" 
		:isShowDate="false" 
		@tap="previewFile(revice)"
		v-if="revice.fileUrl">
		</FileWrap>
		<view v-else class="nofile">
			暂无回证
		</view>
	</CardWrap>
	<custom-button class="toRecipt" :class="{disabled:!isAllRead}" @tap="alradyRedsucess" v-if="!isReceipt && roleInfo == 'AdministrativeAgencies'">
	 阅读完成去签名
	</custom-button>
	</view>
	</view>
</template>

<script setup>
import { onLoad,onShow } from "@dcloudio/uni-app";
import {seeDocumentDetail} from '@/api/file.js'
import {readComplete,addViewRecord,viewDocument} from '@/api/document.js'
import {getSaveRole} from '@/utils/auth.js'
import {reactive, ref} from "vue"
const isReceipt = ref(false)
const signReceiptFile = ref(false)
const deliverId = ref(null)
const isNeedReady = ref(false)
const isRecordShow = ref(false) //备案查看进入列表处
const roleInfo =ref(getSaveRole()) 
const isAllRead = ref(false)
onLoad((data)=>{
	const {type,id,signReceipt,needRead,showRecord} = data
	isRecordShow.value = showRecord === "true" ? true :false
	signReceiptFile.value = signReceipt
	isNeedReady.value = needRead
	deliverId.value = id
	isReceipt.value = type === "0" || roleInfo.value === 'BureauOfJustice' ? false : true
})
onShow(()=>{
	getDetail(deliverId.value)
})
 const seeFileDetail = (fileName)=>{
	 uni.navigateTo({
	 	url:`/pages/fileDetail/fileDetail?fileName=${fileName}`
	 })
 }
 const sendFileList = ref([]) //送出文书列表
 const revice = reactive({
	 fileUrl:"",
	 fileType:"",
	 
 }) //送达回证列表
 const getDetail = async(id)=>{
	 try{
	 const {code,data:res} = await seeDocumentDetail({id})
	 if(code === 200){
		 sendFileList.value = res.documentUrls.map(val=>{
			 const fileType = val.fileUrl?.split(".").pop()
			 return {
				 ...val,fileType
			 }
		 })
		isAllRead.value = !res.documentUrls.some(val=>{
			 return val.isRead === "0"
		 })
		// 司法局已阅读完成
		const flag = !res.documentUrls.some(val=>{
			return val.isScan === "0"
		})
		if(flag &&  roleInfo.value === 'BureauOfJustice'){
	     viewDocument({
			id
		})
		}
		 revice.fileUrl = res.proofServiceUrl
		 revice.fileType = res.proofServiceUrl?.split(".").pop()
		 revice.fileOriginalName = res.proofServiceName
	 }
	 }catch(e){
	 	//TODO handle the exception
		return Promise.reject(e)
	 }

 }
 // 预览文件或者直接跳转签名
 const previewFile = (item)=>{
	 const {fileOriginalName,fileType,fileUrl,id:deliveryFileId} = item
	 const isToRepict = signReceiptFile.value && ['pdf','ofd'].includes(fileType) ? '1' : '0'
	 let url = isNeedReady.value ?  `/pages/signatureBrand/signatureBrand?deliveryId=${deliverId.value}` : `/pages/fileDetail/fileDetail?fileOriginalName=${fileOriginalName}&fileType=${fileType}&fileUrl=${fileUrl}&isToRepict=${isToRepict}&deliverId=${deliverId.value}&isRecordShow=${isRecordShow.value}&deliveryFileId=${deliveryFileId}&isDownLoad=${isReceipt.value}`
	 uni.navigateTo({
	 	url
	 })
 }
 // 已经阅读完成
 const alradyRedsucess = async()=>{
	 if(!isAllRead) return
 	try{
 	const {code,data:res} = await readComplete({
 		deliveryId:deliverId.value
 	})
 	if(code === 200){
 		if(res){	
 		}
		uni.navigateTo({
			url:`/pages/signatureBrand/signatureBrand?deliveryId=${deliverId.value }`
		})
 	}else{
 		uni.showToast({
 			icon:"error",
 			title:"网络错误！"
 		})
 	}
 	}catch(e){
 		//TODO handle the exception
 		return Promise.reject(e)
 	}
 }
</script>
<style lang="scss" scoped>
.fileList{
	position: relative;
	background-color: #f4faff;
	min-height: 100vh;
	padding: 35rpx;
	.title{
		font-family: PingFangSC, PingFang SC;
		font-weight: 600;
		font-size: 40rpx;
		color: #00041A;
		margin-bottom: 32rpx;
	}
  :deep(.cardWrap){
	  padding-bottom: 1rpx;
  }
  .nofile{
	  padding-bottom: 25rpx;
  }
  .toRecipt{
	  width: 80%;
	  position: absolute;
	  bottom: 200rpx;
	  left: 50%;
	  transform: translateX(-50%);
  }
  .disabled{
	background-color: #D5E1FF !important;
	color: #FFFFFF !important;
  }
}
</style>
